//
//  ZZTNetEngine.m
//  ZZTong
//
//  Created by xy on 13-5-10.
//  Copyright (c) 2013年 handpay. All rights reserved.
//

#import "ZZTongNetEngine.h"

@implementation ZZTongNetEngine
@synthesize verifyVerionStatus;
@synthesize verifyVersionData;
@synthesize lastReqData;

-(void)sendRequest:(NSString*)action withParams:(NSDictionary*)params withDelegate:(id<NetCallBackDelegate>)delegate{
//    LOG(@"http request start date::%@",[Utility getNowDateTest]);
    [self sendRequest:action withParams:params withDelegate:delegate withAddress:APP_URL_NAME withAlertFlag:YES];
}
-(void)sendRequest:(NSString*)action withParams:(NSDictionary*)params withDelegate:(id<NetCallBackDelegate>)delegate withUploadImg:(BOOL)upload{
    NSNetData* netData = [[NSNetData alloc] initLoad:APP_URL_NAME withparams:params withheaders:nil withAction:action withDelegate:delegate withAlert:YES witUploadImg:upload];
    self.lastReqData = netData;
    [[NSNetLib sharedInstance] startRequest:netData];
    [netData release];
}
-(void)sendRequest:(NSString*)action withParams:(NSDictionary*)params withDelegate:(id<NetCallBackDelegate>)delegate withAddress:(NSString*)addrName{
    [self sendRequest:action withParams:params withDelegate:delegate withAddress:addrName withAlertFlag:YES];
}

-(void)sendRequest:(NSString*)action withParams:(NSDictionary*)params withDelegate:(id<NetCallBackDelegate>)delegate withAlertFlag:(BOOL)isShowAlert{
    [self sendRequest:action withParams:params withDelegate:delegate withAddress:APP_URL_NAME withAlertFlag:isShowAlert];
}

-(void)sendRequest:(NSString*)action withParams:(NSDictionary*)params withDelegate:(id<NetCallBackDelegate>)delegate withAddress:(NSString*)addrName withAlertFlag:(BOOL)isShowAlert{
    NSNetData* netData = [[NSNetData alloc] initLoad:addrName withparams:params withheaders:nil withAction:action withDelegate:delegate withAlert:isShowAlert witUploadImg:NO];
    
    if (self.verifyVerionStatus == VerifyVerionStatus_NONE) {
        if ([action isEqualToString:NET_ACTION_VERIFY_VERSION]) {// 第一次的网络请求肯定是版本检测
            self.verifyVersionData = netData;
             [[NSNetLib sharedInstance] startRequest:netData];
        }
    }else if (self.verifyVerionStatus == VerifyVerionStatus_Success){
        self.lastReqData = netData;
        [[NSNetLib sharedInstance] startRequest:netData];
    }else if (self.verifyVerionStatus == VerifyVerionStatus_Failed){
        if (![action isEqualToString:NET_ACTION_VERIFY_VERSION]) { // 版本检测失败的情况下，action不为版本检测时，需要先将请求数据保存，然后执行版本检测
            self.lastReqData = netData;
            
            [[NSNetLib sharedInstance] startRequest:[NSNetData netDataWithData:self.verifyVersionData]];
        }
    }else{
        LOG(@"error status");
    }
    
    [netData release];
}

-(void)setVerifyVerionStatus:(VerifyVerionStatus)status{
    if (status == VerifyVerionStatus_NONE
        || status == VerifyVerionStatus_Failed) {
        verifyVerionStatus = status;
        return;
    }
    
    if (status == VerifyVerionStatus_Success) {
        if (verifyVerionStatus == VerifyVerionStatus_NONE) {
            verifyVerionStatus = VerifyVerionStatus_Success;
            return;
        }
        
        if (verifyVerionStatus == VerifyVerionStatus_Failed) {
            verifyVerionStatus = VerifyVerionStatus_Success;
            if (self.lastReqData) { // 由失败状态变成成功状态，将上次执行的请求重新执行
                [[NSNetLib sharedInstance] startRequest:self.lastReqData];
            }
        }
    }
}

-(void)dealloc{
    self.verifyVersionData = nil;
    self.lastReqData = nil;
    
    [super dealloc];
}
@end
